package com.willian.andrappfw.application;

import android.app.Application;
import android.content.pm.PackageManager;
import android.database.sqlite.SQLiteDatabase;

import com.willian.andrappfw.greendao.DaoMaster;
import com.willian.andrappfw.greendao.DaoSession;
import com.willian.andrappfw.utils.CrashHandler;
import com.willian.andrappfw.utils.Utils;

public class WApplication  extends Application {
    private DaoSession mDaoSession;

    public static class ContextHolder {
        static WApplication ApplicationContext;
        static String versionName;
        static int versionCode;

        private static void initial(WApplication context) {
            ApplicationContext = context;
            try {
                versionName = Utils.getApplicationContext().getPackageManager().getPackageInfo(Utils.getApplicationContext().getPackageName(), 0).versionName;
                versionCode = Utils.getApplicationContext().getPackageManager().getPackageInfo(Utils.getApplicationContext().getPackageName(), 0).versionCode;
            } catch (PackageManager.NameNotFoundException ignored) {

            }
        }

        public static WApplication getContext() {
            return ApplicationContext;
        }

        public static String getVersionName() {
            return versionName;
        }

        public static int getVersionCode() {
            return versionCode;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();

        ContextHolder.initial(this);
        Utils.initHandler();
        DaoMaster.DevOpenHelper mHelper = new DaoMaster.DevOpenHelper(this, "file-db", null);
        SQLiteDatabase db = mHelper.getWritableDatabase();
        DaoMaster mDaoMaster = new DaoMaster(db);
        mDaoSession = mDaoMaster.newSession();

        CrashHandler.getInstance().init(this);
    }

    public DaoSession getDaoSession() {
        return mDaoSession;
    }

}
